package com.pengbei.enshrine.common.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.pengbei.enshrine.R;
import com.pengbei.enshrine.common.dialog.callback.AppUpdateAdapter;
import com.pengbei.enshrine.common.dialog.callback.AppUpdateCallBack;
import com.pengbei.enshrine.common.view.DownLoadProgressBar;

import java.util.List;

public class AppUpdateDialog {
    private Activity context;
    private Dialog dialog;
    private RelativeLayout rl_update_all;
    private RecyclerView rv_update;
    private TextView tv_update_message;
    private DownLoadProgressBar pb_download;
    private ImageView btn_update_cancal;
    private TextView btn_update_confirm;
    private Display display;

    private AppUpdateAdapter adapter;
    public AppUpdateDialog(Activity context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }
    public AppUpdateDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_appupdate, null);

        rl_update_all = (RelativeLayout) view.findViewById(R.id.rl_update_all);
        rv_update = (RecyclerView) view.findViewById(R.id.rv_update);
        tv_update_message = (TextView) view.findViewById(R.id.tv_update_message);
        pb_download = (DownLoadProgressBar) view.findViewById(R.id.pb_download);
        btn_update_cancal = (ImageView) view.findViewById(R.id.btn_update_cancal);
        btn_update_confirm = (TextView) view.findViewById(R.id.btn_update_confirm);

        dialog = new Dialog(context, R.style.AlertDialogStyle);

        dialog.setContentView(view);
        rl_update_all.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth()), LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }

    public AppUpdateDialog show(List<String> msg, String text, AppUpdateCallBack appUpdateCallBack) {
        rv_update.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rv_update.setItemAnimator(new DefaultItemAnimator());
        rv_update.setHasFixedSize(true);
        btn_update_confirm.setText(text);
        if(msg.size()>0){
            adapter = new AppUpdateAdapter(context,msg);
            rv_update.setAdapter(adapter);
        }else {
            msg.add("暂未获取到更新内容");
            adapter = new AppUpdateAdapter(context,msg);
            rv_update.setAdapter(adapter);
        }

        btn_update_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pb_download.setVisibility(View.VISIBLE);
                tv_update_message.setVisibility(View.INVISIBLE);
                btn_update_cancal.setVisibility(View.INVISIBLE);
                btn_update_confirm.setVisibility(View.INVISIBLE);
                rv_update.setVisibility(View.INVISIBLE);
                appUpdateCallBack.Update(pb_download);
            }
        });

        btn_update_cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                appUpdateCallBack.Cancal();
            }
        });

        return this;
    }

    public void show() {
        dialog.show();
    }

    public boolean isShowing(){
        if(dialog != null) {
            return dialog.isShowing();
        } else {
            return false;
        }
    }
}